﻿/// <summary>
/// Main form class
/// </summary>
namespace CamoteqItemWizard
{
    using System;
    using System.Configuration.Assemblies;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;

    using Pamela.Logic.Helpers;
    using Pamela.Logic.Scaffolding.Engines;    

    /// <summary>
    /// Main form class
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// The assembly of the shared layer
        /// </summary>
        private Assembly assembly;

        /// <summary>
        /// Builder  object containing all the info to necessary to perform the scaffolding operation
        /// </summary>
        private BuilderInfo builderInfo;

        /// <summary>
        /// Initializes a new instance of the MainForm class
        /// </summary>
        /// <param name="solutionDirectory">Directory of the current solution</param>
        /// <param name="rootNamespace">Root namespace of the project</param>
        public MainForm(string solutionDirectory, string rootNamespace)
        {
            this.builderInfo = new BuilderInfo { RootNamespace = rootNamespace, SolutionDirectory = solutionDirectory }; 
            this.assembly = Assembly.Load(File.ReadAllBytes(this.builderInfo.SharedAssemblyPath));
            this.InitializeComponent();            
        }

        /// <summary>
        /// Form load event handler
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments object</param>
        private void FormLoad(object sender, EventArgs e)
        {
            Type[] types;

            try
            {
                types = this.assembly.GetTypes();                
            }
            catch (ReflectionTypeLoadException ex)
            {
                types = ex.Types;
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                FormCancel(sender, e);
                return;
            }

            var modelTypes = types.Where(o => o != null && o.Namespace == this.builderInfo.ModelNamespace);

            if (modelTypes.Count() == 0)
            {
                throw new Exception("There are no data models to scaffold");
            }
            else
            {
                foreach (var t in modelTypes)
                {
                    cbModels.Items.Add(t.Name);
                }
            }
        }

        /// <summary>
        /// Button OK event handler
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments object</param>
        private void FormOk(object sender, EventArgs e)
        {
            if (cbModels.SelectedItem == null || string.IsNullOrEmpty(cbModels.SelectedItem.ToString()))
            {
                MessageBox.Show("No model selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {                    
                    this.builderInfo.SelectedModel = cbModels.SelectedItem.ToString();
                    var engine = new MvcScaffoldingEngine(this.assembly, this.builderInfo);
                    engine.Build();

                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    FormCancel(sender, e);
                }
            }
        }

        /// <summary>
        /// Button Cancel event handler
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments object</param>
        private void FormCancel(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
